﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SZSLSystem.Common;

namespace SZSLSystem.Web.Admin.Role
{
    public partial class Add : SZSLSystem.Web.UI.ManagePage
    {
        #region 全局变量
        /// <summary>
        /// 组织机构编号
        /// </summary>
        public string orgid;
        /// <summary>
        /// 组织机构名称
        /// </summary>
        public string orgName;
        #endregion 

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (CurrentAdminName.ToLower() == "administrator")
                {
                    AuthorityBind(this.chkAuthority);
                }
                else
                {
                    AuthorityBind(CurrentAdminName, this.chkAuthority);
                }
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //
                //角色
                //
                SZSLSystem.Model.Role model = new SZSLSystem.Model.Role();
                SZSLSystem.BLL.Role bll = new SZSLSystem.BLL.Role();
                Guid ID = Guid.NewGuid();
                if (bll.Exists(ID))
                {
                    JscriptPrint("角色编号重复，请重新操作！", "", "Error");
                    return;
                }
                model.ID = ID;
                model.RoleName = this.txtName.Text.Trim();
                model.Description = this.txtDescription.Text.Trim();
                bll.Add(model);

                SZSLSystem.BLL.RoleAuthority aubll = new BLL.RoleAuthority();
                for (int i = 0; i < this.chkAuthority.Items.Count; i++)
                {
                    if (this.chkAuthority.Items[i].Selected)
                    {
                        SZSLSystem.Model.RoleAuthority aumodel = new Model.RoleAuthority();
                        aumodel.ID = Guid.NewGuid();
                        aumodel.RoleID = model.ID;
                        aumodel.AuthorityID = Guid.Parse(this.chkAuthority.Items[i].Value.ToString());
                        aumodel.Sort = i + 1;
                        aubll.Add(aumodel);
                    }
                }
                //
                //保存日志
                //
                SaveLogs("添加系统角色成功！", 0);

                JscriptPrint("添加组系统角色成功啦！", "List.aspx?OrgID=" + Request.Params["OrgID"], "Success");

            }
            catch(Exception err)
            {
                //
                //异常日志写入
                //
                SaveLogs(err.ToString(), 1);

                JscriptMsg(350, 230, "错误提示", "<b>出现错误了！</b>添加系统角色失败啦！", "", "Error");
            }
        }
    }
}
